package main

import (
	"chrent.com/ServiceManage/boot"
	"chrent.com/ServiceManage/boot/setting"
	"chrent.com/ServiceManage/pm2"
	"chrent.com/ServiceManage/utils"
	"embed"
	"flag"
	"fmt"
	"github.com/ntt360/pmon2/app"
	"github.com/ntt360/pmon2/app/god"
	"io/fs"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path"
)

func main() {
	handleflag()
	e := boot.Setup()
	e.StaticFS("/www", getFileSystem())
	err := app.Instance(utils.GetPm2Conf())
	if err != nil {
		fmt.Println(err)
		return
	}
	if setting.Conf.Production {
		go god.NewMonitor()
	}
	pm2.ReloadStatus()
	e.Run(":" + setting.Conf.Port)
}

//go:embed www
var embededFiles embed.FS

func handleflag() {
	//处理service文件
	s := ""
	flag.StringVar(&s, "s", "", "-s=install 安装服务;-s=remove 移除服务")
	flag.Parse()
	switch s {
	case "install":
		install()
		os.Exit(0)
	case "remove":
		remove()
		os.Exit(0)
	}
}
func install() {
	service := `[Unit]
Description=Service Manage
After=network.target
[Service]
Type=simple
ExecStart=%s
ExecStop=/usr/bin/kill $MAINPID
TimeoutStartSec=0
RemainAfterExit=no
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=default.target
`
	service = fmt.Sprintf(service, path.Join(utils.GetRootDir(), "ServiceManage"))
	seriPath := "/etc/systemd/system/ServiceManage.service"
	//处理配置文件
	config := `gobin: "%s"
production: %s
port: "%d"`
	fmt.Print("输入go文件目录(可执行文件):")
	gobin := ""
	fmt.Scanf("%s", &gobin)
	_, err := os.Stat(gobin)
	if err != nil {
		panic("go不存在:" + err.Error())
	}
	fmt.Print("输入产品模式(true/false):")
	production := ""
	fmt.Scanf("%s", &production)
	if production != "true" && production != "false" {
		panic("产品模式值错误")
	}
	fmt.Print("输入服务端口号(1～65535):")
	port := 0
	fmt.Scanf("%d", &port)
	if port < 1 || port > 65535 {
		panic("端口号错误")
	}
	config = fmt.Sprintf(config, gobin, production, port)
	//处理pmon2config
	pmon2conf := `data: "%s"
logs: "%s"`
	pmon2conf = fmt.Sprintf(pmon2conf, path.Join(utils.GetRootDir(), "data"), path.Join(utils.GetRootDir(), "logs"))
	//正式处理相关文件
	////写入配置文件
	os.Remove(utils.GetConf())
	cfile, err := os.Create(utils.GetConf())
	if err != nil {
		panic("配置文件创建异常!" + err.Error())
	}
	defer cfile.Close()
	_, err = cfile.WriteString(config)
	if err != nil {
		panic("写入配置文件异常!" + err.Error())
	}
	////写入pmon2conf.yml
	os.Remove(utils.GetPm2Conf())
	pm2file, err := os.Create(utils.GetPm2Conf())
	if err != nil {
		panic("创建pmon2conf配置文件异常!" + err.Error())
	}
	defer pm2file.Close()
	_, err = pm2file.WriteString(pmon2conf)
	if err != nil {
		panic("写入pmon2conf配置文件异常:" + err.Error())
	}
	////写入service文件
	os.Remove(seriPath)
	sfile, err := os.Create(seriPath)
	if err != nil {
		panic("创建" + seriPath + "异常:" + err.Error())
	}
	defer sfile.Close()
	_, err = sfile.WriteString(service)
	if err != nil {
		panic("写入service文件异常:" + err.Error())
	}
	fmt.Println("服务安装成功,服务名:ServiceManage;Install success,ServiceName:ServiceManage")
}
func remove() {
	stop := exec.Command("/usr/bin/systemctl", "stop", "ServiceManage")
	err := stop.Run()
	if err != nil {
		panic("停止服务异常:" + err.Error())
	}
	disSer := exec.Command("/usr/bin/systemctl", "disable", "ServiceManage")
	err = disSer.Run()
	if err != nil {
		panic("关闭服务异常:" + err.Error())
	}
	err = os.Remove("/etc/systemd/system/ServiceManage.service")
	if err != nil {
		panic("删除服务异常:" + err.Error())
	}
	fmt.Println("服务移除成功;Install success")
}
func getFileSystem() http.FileSystem {
	log.Print("using embed mode\n")
	fsys, err := fs.Sub(embededFiles, "www")
	if err != nil {
		panic(err)
	}

	return http.FS(fsys)
}
